using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using ShoppingCenter.Backend.Api.Model;
using ShoppingCenter.Backend.Api.Params;
using ShoppingCenter.Backend.Api.Repository;
using ShoppingCenter.Backend.Api.Utils;

namespace ShoppingCenter.Backend.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]

    //购物车控制器
    public class ShoppingCartsController : ControllerBase
    {
        private IConfiguration _configuration;

        private IRepository<Shopping_Cart> _shoppingCartRepository;
         private IRepository<User> _usersRepository;
       
        //商品
        private IRepository<Merchandise> _merchandiseRepository;

        public ShoppingCartsController(IConfiguration configuration,IRepository<Merchandise> merchandiseRepository,IRepository<User> usersRepository ,IRepository<Shopping_Cart> shoppingCartRepository)
        {
            _configuration = configuration;
            _shoppingCartRepository = shoppingCartRepository;
            _usersRepository=usersRepository;
            _merchandiseRepository=merchandiseRepository;
        }



        //获取所有购物车
        public dynamic Get()
        {
            var currentPage = int.Parse(Request.Query["currentPage"][0]);
            var pageSize = int.Parse(Request.Query["pageSize"][0]);

            var shoppingCarts = _shoppingCartRepository.Table.ToList();
            // 分页
           var shoppingCart=shoppingCarts.ToList().Skip((currentPage - 1) * pageSize).Take(pageSize); ;

            return new
            {
                Code = 1000,
                Data = new
                {
                    Data = shoppingCart,
                    Pager = new { currentPage, pageSize, totalSize = shoppingCarts.Count() }
                },
                Msg = "获取所有用户的购物车成功"
            };
        }

        [HttpGet("{userId}")]
        //根据用户id获取购物车
        public dynamic Get(int userId)
        {
            var currentUserShoppingCar = _shoppingCartRepository.GetById(userId);

            return new
            {
                Code = 200,
                Data = currentUserShoppingCar,
                Msg = "获取指定用户的购物车成功"
            };

        }

        //添加购物车
        [HttpPost]
        public dynamic Post(CUR_ShoppingCart newShoppingCart)
        {   
        // 当前用户id、当前商品id
        var user=_usersRepository.GetById(newShoppingCart.UserId);
        var merchandise=_merchandiseRepository.GetById(newShoppingCart.MerchandiseId);

        if (user==null||merchandise==null)
        {
            return new
            {
                Code = 104,
                Data = "",
                Msg = "当前用户或商品不存在"
            };
        }
        //购买数量
        var buyNum=newShoppingCart.BuyNum==0?1:newShoppingCart.BuyNum;
          
          var   ShoppingCart=new Shopping_Cart{
UserId=user.Id,
MerchandiseId=merchandise.Id,
BuyNum=buyNum
          };
           
           _shoppingCartRepository.Insert(ShoppingCart);


         return new
            {
                Code = 1000,
                Data = "",
                Msg = "加入购物车成功"
            };




        // 将结果实例化后输出
            
        }



    }
}